home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3546 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointers to member functions HOW?
  5. Date: 24 Jan 1996 19:23:18 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan24142318@g7240065.bridge.bst.bls.com>
  8. References: <31067074.6B53@compuserve.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Roberto Ortiz's message of Wed, 24 Jan 1996 13:46:28 -0400
  11.  
  12. In article <31067074.6B53@compuserve.com> Roberto Ortiz <74011.3205@compuserve.com> writes:
  13.  
  14. : I've been attempting for some time now to declare a pointer to a class' 
  15. : memeber function. I have no problem with pointers to regular functions, but 
  16. : with class members, I run into one of two problems:
  17.  
  18. : a) I can't assign the function to the pointer.
  19.  
  20. You cannot assign a class member function to a regular function pointer of
  21. the same signature, unless the class member function is static.
  22.  
  23. : b) I can't call the pointer as a function.
  24.  
  25. You require an instance to call the pointer to member function on - using
  26. the operator '.*' (or '->*' for a pointer to an instance).
  27.  
  28. : #include <stdio.h>
  29.  
  30. : void RegularFunction(int iVal)
  31. : {
  32. :       printf("[%d]\n", iVal);
  33. : };
  34.  
  35. : class TTest {
  36. : public:
  37. :       void MemberFunction(int iVal)
  38. :       {
  39. :           printf("[%d]\n", iVal);
  40. :       };
  41. : };
  42.  
  43. : void main()
  44.  
  45. This should be:
  46.   int main(void)
  47.  
  48. : {
  49. :       typedef void (TFuncVoidInt)(int);
  50.  
  51. :       TFuncVoidInt    *Func0;
  52. :       TFuncVoidInt    *Func1;
  53.  
  54. :       void (TTest::*Func2)(int);
  55.  
  56. :       Func0 = RegularFunction;
  57. :       Func1 = TTest::MemberFunction;    // Error;
  58. :       Func2 = &TTest::MemberFunction;
  59.                    ^ this is unnecessary, but not wrong
  60.  
  61. :       Func0(100);
  62. :       Func1(100);
  63. :       Func2(100);                // Error
  64. try:
  65.            TTest test;
  66.            (test.*)Func2(100);
  67. : };
  68.  
  69. : Any suggestions?
  70.  
  71. Above.
  72.  
  73. Regards
  74.   -A.
  75. -- 
  76. | A.Champion                |
  77.